<?php
if (!isset($_SESSION['username'])) {
	header('location:/setup/');
}

require('file_txt.inc.php');
getConfig('./config.txt');
$p1size = count($page1_title);
$p2size = count($page2_title);
$p1count = $p1size < 3 ? 3 : $p1size;
$p2count = $p2size < 3 ? 3 : $p2size;

if(count($page_name)==0) {

	$page_name = ['',''];
}

if ($page_name[0]!= '')
	$navpage1 = $page_name[0] . "导航页";
else
	$navpage1 = "导航页面1";

if ($page_name[1] != '')
	$navpage2 = $page_name[1] . "导航页";
else
	$navpage2 = "导航页面2";

?>


<!DOCTYPE html>
<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge" />
	<meta name="renderer" content="webkit" />
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
	<link rel="shortcut icon" href="favicon.ico" />
	<link rel="stylesheet" href="layui/css/layui.css">
	<script src="js/jquery-1.8.3.min.js" type="text/javascript"></script>

	<title>登录</title>
</head>

<body>

	<div class="layui-container">

		<div style="display: none;">
			<button type="button" class="layui-hide" id="test1"></button>
			<textarea id="editor_id" name="v_content1" style="height:100px;"></textarea>
			<input type="text" id="img_url">
		</div>

		<div class="layui-row">
			<div class="layui-col-xs6">
				<h1>导航网站设置</h1>
			</div>
			<div class="layui-col-xs6" style="text-align: right;">
				<form id="upload_from" action="editor/php/upload_json.php" style="display:none" method="post" enctype="multipart/form-data">
					<input type="file" name="imgFile" value="">
				</form>
				<button class="layui-btn menu1">
					<?= $_SESSION['username'] ?>
					<i class="layui-icon layui-icon-down layui-font-12"></i>
				</button>
			</div>
		</div>


		<br>
		<form class="layui-form" action="submit.php" enctype="multipart/form-data" method="post">

			<div class="layui-tab layui-tab-card">
				<ul class="layui-tab-title">
					<li class="layui-this">网站设置</li>
					<li><?= $navpage1 ?></li>
					<li><?= $navpage2 ?></li>
				</ul>
				<div class="layui-tab-content">
					<div class="layui-tab-item layui-show">

						<div class="layui-form-item">
							<label class="layui-form-label">网页标题</label>
							<div class="layui-input-block">
								<input type="text" name="home_title" lay-verify="home_title" autocomplete="off" class="layui-input" value="<?= $home_title ?>">
							</div>
						</div>

						<div class="layui-form-item">
							<label class="layui-form-label">导航标题</label>
							<div class="layui-input-block">
								<input type="text" name="title" lay-verify="title" autocomplete="off" class="layui-input" value="<?= $title ?>">
							</div>
						</div>

						<div class="layui-form-item">
							<label class="layui-form-label">翻页按钮1</label>
							<div class="layui-input-block">
								<input type="text" name="pagename1" lay-verify="pagename1" autocomplete="off" class="layui-input" value="<?= $page_name[0] ?>">
							</div>
						</div>

						<div class="layui-form-item">
							<label class="layui-form-label">翻页按钮2</label>
							<div class="layui-input-block">
								<input type="text" name="pagename2" lay-verify="pagename2" autocomplete="off" class="layui-input" value="<?= $page_name[1] ?>">
							</div>
						</div>
					</div>
					<div class="layui-tab-item">

						<div id="page1">

							<?php for ($i = 1; $i <= $p1count; $i++) : ?>
								<?php
								if ($i <= $p1size) {
									$title = $page1_title[$i - 1];
									$img = $page1_img[$i - 1];
									$url = $page1_url[$i - 1];
								} else {
									$title = "";
									$img = "";
									$url = "";
								}
								?>
								<div class="layui-form-item">
									<div class="layui-inline">
										<label class="layui-form-label">导航名称<?= $i ?></label>
										<div class="layui-input-inline">
											<input type="text" name="page1name[]" autocomplete="off" class="layui-input" value="<?= $title ?>">
										</div>
									</div>
									<div class="layui-inline">
										<label class="layui-form-label">图像文件</label>
										<div class="layui-input-inline">
											<input type="text" id="page1img<?= $i ?>" name="page1img[]" id="date" autocomplete="off" class="layui-input" value="<?= $img ?>">
										</div>
									</div>
									<div class="layui-inline">
										<button type="button" id="page1up<?= $i ?>" data-name="page1img<?= $i ?>"  class="layui-btn layui-btn-normal pic_sel">选择图片</button>
									</div>

									<div class="layui-inline">
										<label class="layui-form-label">跳转链接</label>
										<div class="layui-input-inline">
											<input type="tel" name="page1url[]" autocomplete="off" class="layui-input" value="<?= $url ?>">
										</div>
									</div>
									<div class="layui-inline">
										<button onclick="moveup(1,<?= $i ?>)" type="button" title="向上调整顺序" class="layui-btn layui-btn-primary layui-btn-sm"><i class="layui-icon layui-icon-up"></i></button>
									</div>
								</div>
							<?php endfor; ?>
						</div>
						<div class="layui-btn-container" style="text-align: center;">
							<button id="addpage1" onclick="additem('#page1',1)" type="button" class="layui-btn layui-btn-danger layui-btn-sm"><i class="layui-icon"></i></button>
						</div>

					</div>
					<div class="layui-tab-item">

						<div id="page2">
							<?php for ($i = 1; $i <= $p2count; $i++) : ?>
								<?php

								if ($i <= $p2size) {
									$title = $page2_title[$i - 1];
									$img = $page2_img[$i - 1];
									$url = $page2_url[$i - 1];
								} else {
									$title = "";
									$img = "";
									$url = "";
								}
								?>
								<div class="layui-form-item">
									<div class="layui-inline">
										<label class="layui-form-label">导航名称<?= $i ?></label>
										<div class="layui-input-inline">
											<input type="text" name="page2name[]" autocomplete="off" class="layui-input" value="<?= $title ?>">
										</div>
									</div>
									<div class="layui-inline">
										<label class="layui-form-label">图像文件</label>
										<div class="layui-input-inline">
											<input type="text" id="page2img<?= $i ?>" name="page2img[]" id="date" autocomplete="off" class="layui-input" value="<?= $img ?>">
										</div>
									</div>

									<div class="layui-inline">
										<button type="button" id="page2up<?= $i ?>" data-name="page2img<?= $i ?>" class="layui-btn layui-btn-normal pic_sel">选择图片</button>
									</div>
									<div class="layui-inline">
										<label class="layui-form-label">跳转链接</label>
										<div class="layui-input-inline">
											<input type="tel" name="page2url[]" autocomplete="off" class="layui-input" value="<?= $url ?>">
										</div>
									</div>
									<div class="layui-inline">
										<button onclick="moveup(2,<?= $i ?>)" type="button" title="向上调整顺序" class="layui-btn layui-btn-primary layui-btn-sm"><i class="layui-icon layui-icon-up"></i></button>
									</div>
								</div>
							<?php endfor; ?>

						</div>
						<div class="layui-btn-container" style="text-align: center;">
							<button id="addpage2" onclick="additem('#page2',2)" type="button" class="layui-btn layui-btn-danger layui-btn-sm"><i class="layui-icon"></i></button>
						</div>

					</div>

				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button type="submit" class="layui-btn" lay-submit="">立即提交</button>
					<button type="reset" class="layui-btn layui-btn-primary">重置</button>
				</div>
			</div>
		</form>
	</div>

	<!-- 引入 layui.js 的 <script> 标签最好放置在 html 末尾 -->
	<script src="layui/layui.js"></script>
	<?php
	if (isset($_SESSION['msg'])) {
		echo '<script>  layer.msg("' . $_SESSION['msg'] . '");</script>';
		unset($_SESSION['msg']);
	}
	?>

	<script>
		layui.extend({'fileManager': 'fileManager/ext/fileManager'});
		layui.use(['dropdown', 'jquery', 'layer','fileManager'], function() {
			var layer = layui.layer,
				form = layui.form,
				dropdown = layui.dropdown,
				element = layui.element,
				$ = layui.$ //重点处
				,
				util = layui.util;

			dropdown.render({
				elem: '.menu1',
				data: [{
					title: '修改密码',
					id: 100
				}, {
					title: '注　　销',
					id: 101
				}],
				click: function(obj) {
					if (obj.id == 100) {
						layer.open({
							type: 1,
							resize: false,
							shadeClose: true,
							area: '350px',
							title: '修改密码',
							content: ['<ul class="layui-form layui-form-pane" style="margin: 15px;">', '<li class="layui-form-item">', '<label class="layui-form-label">输入新密码</label>', '<div class="layui-input-block">', '<input type="password" class="layui-input" lay-verify="required" name="field1">', '</div>', '</li>', '<li class="layui-form-item" style="text-align:center;">', '<button type="submit" lay-submit lay-filter="*" class="layui-btn">提交</button>', '</li>', '</ul>'].join(''),
							success: function(layero, index) {
								layero.find('.layui-layer-content').css('overflow', 'visible');

								form.render().on('submit(*)', function(data) {

									layer.close(index); //关闭层
									$.ajax({
										url: "setpass.php",
										data: {
											'newpass': data.field.field1
										},
										headers: {
											'Content-Type': 'application/te;charset=utf-8'
										}, //接口json格式
										success: function(data) {
											layer.msg(data);
										},
										error: function(data) {
											layer.alert("密码修改错误。");
										}
									});
								});
							}
						});

					} else {

						window.location.href = "logout.php";

					}
				}
			});

			var fileManager = layui.fileManager
            , $ = layui.$
            , upload = layui.upload
            , layer = layui.layer;
			$('title').html($('title').html() + ' version:' + fileManager.v);
			var upIns = upload.render({
				elem: '#test1' //绑定元素
				, url: 'data.php?action=upload' //上传接口
				, field: 'file[]'
			});



			bindclick('.pic_sel');

			//弹层选择后的回调
			window.callback = function(res){
				let name = "#"+ layui.data('_fm')._picview_name;
				console.log(name)
				$(name).val("/image" + res.path);
				layui.data('_fm',null);
			}
		});

		function bindclick(id){
			//弹层选择
			$(document).on('click',id,function(res){
			let name = $(this).data('name');
			layui.data('_fm', {key: '_picview_name',value: name});
			layer.open({
				type:2
				,title: "选择图片"
				, area: ['620px', '600px']
				,content: ['fileManager/pop.php', 'no']
				})
			});
		}

		function moveup(page_id, pos) {
			if(pos<=1) layer.msg("已经是第一项了。");
			var id = "#page" + page_id + " .layui-form-item";
			var items =  $(id).slice(pos-2, pos);	
			if(items.length!=2) return;
			inputs1 = items.eq(0).find("input");
			inputs2 = items.eq(1).find("input");

			var t1 = inputs1.eq(0).val();
			var t2 = inputs1.eq(1).val();
			var t3 = inputs1.eq(2).val();
			inputs1.eq(0).val(inputs2.eq(0).val());
			inputs1.eq(1).val(inputs2.eq(1).val());
			inputs1.eq(2).val(inputs2.eq(2).val());
			
			inputs2.eq(0).val(t1);
			inputs2.eq(1).val(t2);
			inputs2.eq(2).val(t3);
		}

		function additem(page_id, page) {
			var id = page_id + " .layui-form-item";
			var pos = $(id).length + 1;
			$(page_id).append('<div class="layui-form-item">\
							<div class="layui-inline">\
								<label class="layui-form-label">导航名称' + pos + '</label>\
								<div class="layui-input-inline">\
									<input type="text" name="page' + page + 'name[]" autocomplete="off" class="layui-input" value="">\
								</div>\
							</div>\
							<div class="layui-inline">\
								<label class="layui-form-label">图像文件</label>\
								<div class="layui-input-inline">\
									<input type="text" id="page' + page + 'img' + pos + '" name="page' + page + 'img[]" id="date" autocomplete="off" class="layui-input" value="">\
								</div>\
							</div>\
							<div class="layui-inline">\
								<button type="button" id="page' + page + 'up' + pos + '" data-name="page' + page + 'img' + pos + '" class="layui-btn layui-btn-normal">选择图片</button>\
							</div>\
							<div class="layui-inline">\
								<label class="layui-form-label">跳转链接</label>\
								<div class="layui-input-inline">\
									<input type="tel" name="page' + page + 'url[]" autocomplete="off" class="layui-input" value="">\
								</div>\
							</div>\
							<div class="layui-inline">\
										<button onclick="up(' + page + ',' + pos + ')" type="button" title="向上调整顺序" class="layui-btn layui-btn-primary layui-btn-sm"><i class="layui-icon layui-icon-up"></i></button>\
									</div>\
						</div>');
			bindclick('#page' + page + 'up' + pos);
			
		}
	</script>

</body>

</html>